var utils = require("../newUtils/utils");
var time = require("../newUtils/time");
var defaultActionConfig = {
  per: 0, //每个多少秒执行一次
  records: [],
  startTime: null, //本次开始时间
  endTime: null, //本次结束时间
  execCount: 0, //今日执行次数
  execCost: 0, //本次执行时间,
};
function ACTION(name, config) {
  this.name = name;
  utils.merge(this, config || {}, defaultActionConfig);
  this.records = [];
}
ACTION.prototype.goStart = function () {
  console.log(this.name, time.str(), "goStart");
  return true;
};
ACTION.prototype.beforeExec = function () {
  this.startTime = Date.now();
  console.log(this.name, time.str(), "beforeExec");
  return true;
};
ACTION.prototype.exec = function () {};
ACTION.prototype.afterExec = function () {
  this.endTime = Date.now();
  this.execCost = this.endTime - this.startTime;
  this.execCount = this.execCount + 1;
  console.log(
    this.name,
    time.str(),
    "执行-" + this.execCount + "-次",
    "耗时-" + this.execCost / 1000 + "-s"
  );
};
ACTION.prototype.beforeEnd = function () {
  console.log(this.name, time.str(), "beforeEnd");
};
ACTION.prototype.end = function () {
  console.log(this.name, time.str(), "end");
};
ACTION.prototype.afterEnd = function () {
  console.log(this.name, time.str(), "afterEnd");
  // this.records.push({
  //   execCount: this.execCount,
  //   startTime: this.startTime,
  //   endTime: this.endTime,
  //   execCost: this.execCost,
  // });
};
ACTION.prototype.run = function () {
  if (this.goStart()) {
    var beforeExec = this.beforeExec();
    if (beforeExec) {
      this.exec(beforeExec);
      this.afterExec();
      this.beforeEnd();
      this.end();
      this.afterEnd();
    }
  } else {
    console.log(this.name, "到达开始页面失败");
  }
};

module.exports = ACTION;
